/*
 * Project Beknyou
 * Copyright (c) 2010-2011 Saint Paul College, All Rights Reserved
 * Redistributions in source code form must reproduce the above
 * Copyright and this condition.
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 */
package com.benkyou.util;

import java.util.ArrayList;
/**
 * 
 * @author Austin Allman
 */

public class ByteArrayList {

    private byte[] joinedBytes;
    public static final byte[] JFIF_HEADER = {2, 5, 5, 2, 2, 4, 0, 1, 6, 7, 4, 7, 0, 7, 3, 7, 0};
    public static final int MESSAGE_LIMIT = 32500;
    private ArrayList<byte[]> byteList = new ArrayList<byte[]>();
    public int length = 0;

    public boolean addHeader(byte[] header) {
        if (!byteList.contains(header)) {
            byteList.add(0, header);
            length += header.length;
            return true;
        } else {
            return false;
        }
    }

    public void addBytes(byte[] bytesToAdd) {
        length += bytesToAdd.length;
        byteList.add(bytesToAdd);
    }

    public boolean removeBytes(byte[] bytesToRemove) {
        if (byteList.remove(bytesToRemove)) {
            length -= bytesToRemove.length;
            return true;
        } else {
            return false;
        }
    }

    public boolean removeAllBytes(byte[] bytesToRemove) {
        if (byteList.remove(bytesToRemove)) {
            length -= bytesToRemove.length;
            return removeAllBytes(bytesToRemove);
        } else {
            return false;
        }
    }

    public void clearArrayList() {
        length = 0;
        byteList.clear();
    }

    public ArrayList<byte[]> getByteArrayList() {
        return byteList;
    }

    public byte[] getByteArray() {
        joinedBytes = new byte[length];
        int counter = 0;
        for (byte[] b : byteList) {
            for (int i = 0; i < b.length; i++) {
                joinedBytes[counter] = b[i];
                ++counter;
            }
        }
        return joinedBytes;
    }

    public ArrayList<byte[]> getDividedList(int limit) {
        ArrayList<byte[]> dividedBytes = new ArrayList<byte[]>();
        byte[] temp = getByteArray();
        byte[] buffer = new byte[limit];
        int tempCounter = 0;
        for (int i = 0; i < temp.length; i++) {
            buffer[tempCounter] = temp[i];
            tempCounter++;
            if (i % limit == 1) {
                int remaining = temp.length - i;
                if (remaining < limit) {
                    dividedBytes.add(buffer);
                    buffer = new byte[remaining];
                    tempCounter = 0;
                } else {
                    dividedBytes.add(buffer);
                    buffer = new byte[limit];
                    tempCounter = 0;
                }
            }
        }
        return dividedBytes;
    }

    public ArrayList<byte[]> divideArrayList(int limit, byte[] byteList) {
        ArrayList<byte[]> dividedBytes = new ArrayList<byte[]>();
        byte[] temp = byteList;
        byte[] buffer = new byte[limit + 1];
        int tempCounter = 0;
        boolean maxDividen = false;
        for (int i = 0; i < temp.length; i++) {
            buffer[tempCounter] = temp[i];
            if (i % limit == 0 && i != 0) {
                int remaining = temp.length - i;
                if (remaining < limit) {
                    dividedBytes.add(buffer);
                    buffer = new byte[remaining];
                    tempCounter = 0;
                } else {
                    dividedBytes.add(buffer);
                    buffer = new byte[limit + 1];
                    tempCounter = 0;
                }
            } else if (i == temp.length - 1) {
                dividedBytes.add(buffer);
            }
            tempCounter++;
        }
        return dividedBytes;
    }

    public void setList(ArrayList<byte[]> temp) {
        byteList = temp;
        length = temp.size();
    }
}